/* 
 * Copyright 2011 kOSMik.
 * This file is part of kOSMik.
 * 
 * kOSMik is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * kOSMik is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * kOSMik:
 * Open Street Map - Editor.
 * http://code.google.com/p/swp-dv-ws2010-osm-2/
 */

package de.fuberlin.kosmik.gui.view;

import java.util.Collection;
import java.util.List;

import org.mapsforge.android.maps.ArrayWayOverlay;
import org.mapsforge.android.maps.GeoPoint;
import org.mapsforge.android.maps.OverlayWay;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import de.fuberlin.kosmik.R;

/**
 * Collection of different static methods to be used for different View
 * procedures.
 * 
 * @author amit
 */
public final class ViewUtils {

	private ViewUtils() {
		// prevent creation of an instance
	}

	/**
	 * Create a {@link Paint} to be used for displaying a track or a way on the
	 * MapFrame.
	 * 
	 * @param color int from <code>Color</code>
	 * @param fill if it should be filled or not
	 * @return Paint object to be used on the map
	 * @see Color
	 */
	public static Paint mapPaint(int color, boolean fill) {

		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		if (fill) {
			paint.setStyle(Paint.Style.FILL);
		} else {
			paint.setStyle(Paint.Style.STROKE);
		}
		paint.setColor(color);
		paint.setAlpha(160);
		paint.setStrokeWidth(4);
		paint.setStrokeCap(Paint.Cap.BUTT);
		paint.setStrokeJoin(Paint.Join.ROUND);

		return paint;

	}

	/**
	 * Creates an ArrayWayOverlay.
	 * 
	 * @return ArrayWayOverlay
	 */
	public static ArrayWayOverlay arrayWayOverlay() {

		ArrayWayOverlay result;
		Paint fill;
		Paint outline;

		fill = outline = mapPaint(Color.MAGENTA, false);

		result = new ArrayWayOverlay(fill, outline);

		return result;
	}

	/**
	 * Creates an overlay for the map.
	 * 
	 * @param context context of overlay
	 * @param geoPoints points of this overlay
	 * @param isWay if it is a way or not
	 * @param partOfArea if it is an area or not
	 * @return Overlay
	 */
	public static OverlayWay overlayWay(Context context, List<GeoPoint> geoPoints,
			boolean isWay, boolean partOfArea) {
		String preferencesKey = context.getResources().getString(R.string.preferences_key_map);
		String wayColorKey = context.getResources().getString(R.string.settings_key_way_color);
		int defaultWayColor = context.getResources().getInteger(R.color.map_way_color);

		int wayColor = context.getSharedPreferences(preferencesKey,
				Context.MODE_WORLD_WRITEABLE).getInt(wayColorKey, defaultWayColor);

		GeoPoint[] pointsArray = toGeoPointArray(geoPoints);

		OverlayWay result;

		Paint fill;
		Paint outline;

		if (isWay) {
			if (partOfArea) {
				fill = outline = mapPaint(Color.BLACK, true);
			} else {
				fill = outline = mapPaint(wayColor, false);
			}
		} else {
			fill = outline = mapPaint(Color.MAGENTA, false);
		}

		result = new OverlayWay(pointsArray, fill, outline);

		return result;
	}

	/**
	 * Transforms a collection of GeoPoints to an array.
	 * 
	 * @param collection the geo points
	 * @return array of geo points
	 */
	public static GeoPoint[] toGeoPointArray(Collection<GeoPoint> collection) {

		if (collection == null || collection.size() == 0) {
			return new GeoPoint[] {};
		}

		GeoPoint[] result = new GeoPoint[collection.size()];

		int i = 0;
		for (GeoPoint geoPoint : collection) {
			result[i++] = geoPoint;
		}
		return result;
	}

}
